// pages/join/index.js
const { joinUs, wxPayment, getOpenId } = require('../../api/api.js')
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    topTitle: {
      top: 0,
      height: 0,
    },
    checked: false,
    name: '',
    phone: '',
    idcard: '',
    deposit: '',
    typeSelectIndex: -1,
    typeRange: [{ key: '1', title: '应聘', }, { key: '2', title: '培训', }, { key: '3', title: '考证' }, { key: '4', title: '加盟' },]
  },

  lifetimes: {
    // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
    attached: function () {
      const {
        top,
        height
      } = wx.getMenuButtonBoundingClientRect();
      this.setData({
        topTitle: {
          top,
          height
        }
      })
    },
    moved: function () { },
    detached: function () { },
  },
  pageLifetimes: {
    // 组件所在页面的生命周期函数
    show: function () { },
    hide: function () { },
    resize: function () { },
  },
  /**
   * 组件的方法列表
   */
  methods: {
    goBack() {
      wx.navigateBack({
        delta: 1,
      })
    },
    handleTypePickerChange(e) {
      const typeSelectIndex = e.detail.value;
      const selectTypeKey = this.data.typeRange[typeSelectIndex].key;
      const depositMap = {
        '1': 500,
        '2': 1000,
        '3': 2000,
        '4': 10000
      }
      this.setData({
        typeSelectIndex,
        deposit: depositMap[selectTypeKey]
      })
    },
    onCheckChange(event) {
      this.setData({
        checked: event.detail,
      });
    },
    async joinUsSubmit() {
      const { name, phone, idcard, checked, deposit, typeSelectIndex, typeRange } = this.data;
      if (typeSelectIndex < 0) {
        wx.showToast({
          title: '请选择类型',
          icon: 'none',
          duration: 1000
        })
        return;
      }
      if (!name) {
        wx.showToast({
          title: '请输入您的姓名',
          icon: 'none',
          duration: 1000
        })
        return;
      }
      if (!phone) {
        wx.showToast({
          title: '请输入您的电话',
          icon: 'none',
          duration: 1000
        })
        return;
      }
      if (!idcard) {
        wx.showToast({
          title: '请输入您的身份证号',
          icon: 'none',
          duration: 1000
        })
        return;
      }
      if (!checked) {
        wx.showToast({
          title: '请阅读并同意相关协议',
          icon: 'none',
          duration: 1000
        })
        return;
      }
      const amount = this.data.deposit;
      wx.login({
        success: async (res) => {
          let code = res.code
          const { data: { openid: openId } } = await getOpenId({ code })
          const payResult = await wxPayment({
            amout: amount,
            openId
          })
          wx.requestPayment({
            ...payResult.data,
            async success(res) {
              const result = await joinUs({
                amount: deposit,
                idCard: idcard,
                name,
                phone,
                type: typeRange[typeSelectIndex].key
              })
              wx.showToast({
                title: '提交成功',
                icon: 'success',
                duration: 1000,
              })
              setTimeout(() => {
                wx.navigateBack()
              }, 1000)
            },
            fail(res) {
              console.log(res)
            }
          })
        }
      })
    },
    goUserService() {
      wx.navigateTo({
        url: '/pages/userService/index',
      })
    },
    goPrivacy() {
      wx.navigateTo({
        url: '/pages/privacy/index',
      })
    }
  }
})